package cn.e4j.modular.crm.controller;

import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.reqres.response.SuccessResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.LangUtils;
import cn.e4j.core.util.StringUtils;
import cn.e4j.modular.crm.entity.GwefT;
import cn.e4j.modular.crm.service.impl.Gwei007ServiceImpl;
import cn.e4j.modular.erp.controller.ErpLogic;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * 发票
 * @author liuw
 * @date 2019/09/04
 */
@Controller
@RequestMapping ("/crm/gwei007")
public class Gwei007Controller {

    @Autowired
    private Gwei007ServiceImpl gwei007Service;

    @Autowired
    private ErpLogic erpLogic;

    /**
     * 跳转发票页面
     */
    @RequiresPermissions("crm:gwei007:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/crm/gwei007.html";
    }


    /**
     * 查询发票列表
     */
    @RequestMapping("/getList")
    @RequiresPermissions("crm:gwei007:init")
    @ResponseBody
    public ResponseData getList(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();

        List<Dto> result = gwei007Service.getList(page, inDto);
        return new PageReData(page, result);
    }


    /**
     * 发票单身表格数据
     */
    @RequestMapping("/getDetailList")
    @ResponseBody
    public ResponseData getDetailList(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        ShiroUser user = ShiroKit.getUser();

        inDto.put("ENT", user.getDBC());
        inDto.put("LANG", LangUtils.getLang(user.getLANG()));

        List<Dto> resultDto = gwei007Service.getGwefData(inDto);

        // 获取营运中心名称
        inDto.put("BIZ_CODE",resultDto.get(0).getString("GWEFPLANT"));
        Dto azpDto = null;
        try {
            azpDto = erpLogic.getERPService("getAzpFile",inDto);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }catch(Exception e){
            e.printStackTrace();
        }

        if(azpDto != null) {
            resultDto.get(0).put("GWEFPLANT_NAME", azpDto.getString("AZP02"));
        }else {
            resultDto.get(0).put("GWEFPLANT_NAME", "");
        }
        return new SuccessResponseData(resultDto);
    }


    /**
     * 获取开票申请资料
     */
    @RequestMapping("/getGwedData")
    @ResponseBody
    public ResponseData getGwedData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Page page = dtoWapper.getPage();
        ShiroUser shiroUser = ShiroKit.getUser();

        String LANG = LangUtils.getLang(shiroUser.getLANG());
        String DBC = shiroUser.getDBC();
        String USER_ID = shiroUser.getID();

        inDto.put("ENT", DBC);
        inDto.put("LANG", LANG);
        inDto.put("USER_ID", USER_ID);

        if(!StringUtils.isEmpty(inDto.getString("gwedlist"))){
            String[] str = inDto.getString("gwedlist").split(",");
            List<String> plist = Lists.newArrayList();
            for (String gwec : str) {
                plist.add(gwec);
            }
            inDto.put("GWEDLIST",plist);
        }
        List<Dto> resultDto = gwei007Service.getGwedListPage(page,inDto);

        // 对结果进行处理
        for (int i = 0; i < resultDto.size(); i++) {
            String gwec003 = resultDto.get(i).getString("GWEC003");
            if(StringUtils.isNotEmpty(gwec003)){
                Dto eDto= Dtos.newDto();
                eDto.put("GWCSENT",shiroUser.getDBC());
                eDto.put("GWCSDOCNO",resultDto.get(i).getString("GWEC003"));//订单号
                eDto.put("GWCSSEQ",resultDto.get(i).getString("GWEC101"));

                Dto gwcs = gwei007Service.getGwcsData(eDto);
                if(gwcs != null) {
                    resultDto.get(i).put("GWAP014",gwcs.getString("GWCS032"));//税种
                    resultDto.get(i).put("GWEA006",gwcs.getBigDecimal("GWEA006"));//税率
                    resultDto.get(i).put("GWAP005",gwcs.getString("GWCS006"));//币种
                    resultDto.get(i).put("GWEF007",gwcs.getBigDecimal("GWCS034"));//汇率
                }
            }
        }

        return new PageReData(page,resultDto);
    }


    /**
     * 新增
     */
    @RequestMapping("/addData")
    @RequiresPermissions("crm:gwei007:add")
    @ResponseBody
    public ResponseData addData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwefT> gwefTList = inDto.getReqList(GwefT.class);

        gwei007Service.addData(gwefTList);
        return new SuccessResponseData();
    }

    /**
     * 修改
     */
    @RequestMapping("/editData")
    @RequiresPermissions("crm:gwei007:edit")
    @ResponseBody
    public ResponseData editData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwefT> gwefTList = inDto.getReqList(GwefT.class);

        gwei007Service.editData(gwefTList);
        return new SuccessResponseData();
    }

    /**
     * 删除
     */
    @RequestMapping("/del")
    @RequiresPermissions("crm:gwei007:del")
    @ResponseBody
    public ResponseData del(GwefT gwef) {
        gwei007Service.delete(gwef);
        return new SuccessResponseData();
    }

    /**
     * 批量删除数据
     */
    @RequestMapping("/delDataBatch")
    @RequiresPermissions("crm:gwei007:del")
    @ResponseBody
    public ResponseData deleteDataBatch(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwefT> gwefTList = inDto.getReqList(GwefT.class);
        gwei007Service.deleteBatch(gwefTList);
        return new SuccessResponseData();
    }


    /**
     * 审核
     */
    @RequestMapping("/checkDataBatch")
    @RequiresPermissions("crm:gwei007:del")
    @ResponseBody
    public ResponseData review(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwefT> gwefTList = inDto.getReqList(GwefT.class);
        gwei007Service.review(gwefTList);
        return new SuccessResponseData();
    }

    /**
     * 取消审核
     */
    @RequestMapping("/cancelCheckDataBatch")
    @RequiresPermissions("crm:gwei007:del")
    @ResponseBody
    public ResponseData cancelReview(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwefT> gwefTList = inDto.getReqList(GwefT.class);
        gwei007Service.cancelReview(gwefTList);
        return new SuccessResponseData();
    }

    /**
     * 新增时获取默认数据 (生成单号，申请人员，归属组织)
     */
    @RequestMapping("/getAddData")
    @RequiresPermissions("crm:gwei007:init")
    @ResponseBody
    public ResponseData getAddData(@DtoParam DtoWapper dtoWapper) {
        ShiroUser shiroUser = ShiroKit.getUser();
        Dto addDataDto = Dtos.newDto();
        addDataDto.put("GWEF020",shiroUser.getID());  // 申请人员
        addDataDto.put("GWEF020_NAME",shiroUser.getNAME());  // 申请人员
        addDataDto.put("GWEF021",shiroUser.getORG_ID()); // 组织
        addDataDto.put("GWEF021_NAME",shiroUser.getSysOrgT().getNAME());  // 归属组织
        addDataDto.put("GWEFDOCNO","KPXX" + DateUtils.getDate("yyyyMMddSSSSS")); // 单号
        return new SuccessResponseData(addDataDto);
    }



    /**
     * 开票单号开窗数据
     */
    @RequestMapping("/getGwefdocnoListPage")
    @ResponseBody
    public ResponseData getGwefdocnoListPage(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        inDto.put("ENT",shiroUser.getDBC());
        List<Dto> resultDto = gwei007Service.getGwefdocnoListPage(page, inDto);
        return new PageReData(page, resultDto);
    }



    /**
     * 汇率类型、汇率 ( 此方法已被启用，字段被集成到了订单中，直接拿取即可 ）
     */
    @ResponseBody
    @RequestMapping("getGwef007")
    public ResponseData getGwef007(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto outDto = Dtos.newDto();
        Dto resultDto = gwei007Service.getGwak008(inDto);
        Dto gwef019Dto = gwei007Service.getGwef019(inDto);
        inDto.put("TYPE",gwef019Dto.getString("GWEF019"));
        inDto.put("GWEK001",resultDto.getString("GWAK008"));
        Dto gwef007Dto = gwei007Service.getGwef007(inDto);
        outDto.put("GWEF006",resultDto.getString("GWAK008"));
        outDto.put("GWEF019",gwef019Dto.getString("GWEF019"));
        outDto.put("GWEF007",gwef007Dto.getString("GWEF007"));
        outDto.put("GWEF005",resultDto.getString("GWEF005"));
        outDto.put("GWEA006",resultDto.getString("GWEA006"));
        return new SuccessResponseData(resultDto);
    }
}
